<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{$web_name}-域名配置-短网址列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="__HOME__/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__HOME__/static/layuiadmin/style/admin.css" media="all">
    <style type="text/css">
        .layui-table-cell{
            text-align: center;
            height: auto;
            white-space: normal;
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layui-btn-danger layuiadmin-btn-useradmin" data-type="add">删除失效链接</button>
            </div>

            <table id="LAY-items-manage" lay-filter="LAY-items-manage"></table>
            <script type="text/html" id="table-applet-user">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>续签</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-edit"></i>删除</a>
            </script>
            <!--这个是选择框tpl-->
            <script type="text/html" id="show_tpl">
                <input type="checkbox"  user_id="{{d.id}}"  lay-skin="switch" lay-text="激活|封禁"  lay-filter="thisStatus" {{d.status==1?'checked':''}} >
            </script>
        </div>
    </div>
</div>

<script src="__HOME__/static/layuiadmin/layui/layui.js"></script>
<script>
    function dataTime(urlTime){
        return new Date(parseInt(urlTime) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
    }
    function timeOut(end_time){
        let timestamp = Date.parse(new Date());//现在时间(时间戳)
        let now_time = timestamp /1000;//现在时间(时间戳)
        if(end_time < now_time){
            return false;
        }else{
            return true;
        }
    }

    //layer组件
    layui.config({
        base: '__HOME__/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: '/lib/index' //主入口模块
    }).use(['form','index','table'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table;
        //
        form.on('select(thisSearch)', function(data){
            var val=data.value;
            var inputs  = $("#is_val");
            if(val=='thisNoSettle'){
                $(inputs).hide();
            }else{
                $(inputs).show();
            }
        });
        //Ajax设置
        $.ajaxSetup({
            type: 'POST',
            dataType : "json",
            headers: {'X-Requested-With': 'XMLHttpRequest'},
        });
        //table事件开始
        table.render({
            elem: '#LAY-items-manage',
            url:  '{fun:ctr("shortDomainList")}',
            cols: [
                [
                    {field: 'id', title: '标识',  sort: true},
                    {field: 'domain_id', title: '域名ID', hide:true},
                    {field: 'domain', title: '所属短域名'},
                    {field: 'address', title: '短网址'},
                    {field: 'cycle', title: '生命周期/天', sort: true, templet: function(d){
                            if(d.cycle==0){
                                return  '不限制';
                            }else{
                                return  d.cycle + '/天';
                            }
                        }},
                    {field: 'record', title: '点击频率/次'},
                    {field: 'safe', title: '防红守护', sort: true, templet: function(d){
                            if(d.safe==1){
                                return  '<a style="color: #ff0000">守护</a>';
                            }else{
                                return  '<a style="color: #000000">放任</a>';
                            }
                        }},
                    {field: 'create_time', title: '添加/失效时间', sort: true, templet: function(d){
                            return dataTime(d.create_time) + '<br>' + dataTime(d.end_time);
                        }},
                    {title: '备注', sort: true, templet: function(d){
                        let end_time = d.end_time;
                        if(d.cycle == 0 ){
                            return  '<a style="color: #00FF00">正常</a>';
                        }
                        let time = timeOut(end_time);
                        if(time){
                            return  '<a style="color: #00FF00">正常</a>';
                        } else {
                            return  '<a style="color: #ff0000">失效</a>';
                        }
                        }},
                    {title: '操作', toolbar: '#table-applet-user'}
                ]
            ],
            id:'itemList',
            page: true,
            limit: 30,
            height: 'full-220',
            text: '对不起，加载出现异常！',
            done: function (res) {
                if (res.code == -7) {
                    layer.msg(res.msg, {icon: 2});
                    window.setTimeout(function () {
                        top.location = '{fun:ctr("../Login")}';
                    }, 1000);
                }else if (res.code == -2) {
                    layer.msg(res.msg, {icon: 2});
                }
            }
        });
        //table事件结束
        //事件
        var active = {
            add: function(){
                parent.layer.open({
                    type: 2,
                    title: '添加域名',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['65%', '75%'],
                    content: '{fun:ctr("../Iframe/addDomain")}',
                });
            },end:function(){
                location.reload();
            }
        };
        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            layer.confirm('确定删除全部失效短网址吗?', function(index){
                $.ajax({
                    url: '{fun:ctr("delAllShortDomain")}',
                    data:{},
                    success:function(data){
                        if(data.code == 200 ){
                            layer.msg(data.msg, {icon: 1});
                            window.setTimeout(function () {
                                window.location.reload();
                            }, 500);
                        }else if(data.code == -7){
                            layer.msg(data.msg, {icon: 2});
                            window.setTimeout(function () {
                                top.location = '{fun:ctr("../Login")}';
                            }, 1000);
                        }else{
                            layer.msg(data.msg, {icon: 2});
                        }
                    },
                    error: function(data){
                        layer.msg('数据异常，操作失败！');
                    },
                });
            })
        });
        //编辑，删除，发布申请
        //监听行工具事件
        table.on('tool(LAY-items-manage)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                layer.prompt({title: '请输入续签天数，0表示永久', formType: 2}, function(text, index){
                    $.ajax({
                        url: '{fun:ctr("ShortDomainTime")}',
                        data:{id:data.id,cycle:text},
                        success:function(data){
                            if(data.code == 200 ){
                                layer.msg(data.msg, {icon: 1});
                                window.setTimeout(function () {
                                    window.location.reload();
                                }, 500);
                            }else if(data.code == -7){
                                layer.msg(data.msg, {icon: 2});
                                window.setTimeout(function () {
                                    top.location = '{fun:ctr("../Login")}';
                                }, 1000);
                            }else{
                                layer.msg(data.msg, {icon: 2});
                            }
                        },
                        error: function(data){
                            layer.msg('数据异常，操作失败！');
                        },
                    });
                    layer.close(index);
                });
            }else if(obj.event === "del"){
                layer.confirm('确定删除，短网址: /' +data.address +' ?', function(index){
                    $.ajax({
                        url: '{fun:ctr("delShortDomain")}',
                        data:{id:data.id},
                        success:function(data){
                            if(data.code == 200 ){
                                layer.msg(data.msg, {icon: 1});
                                window.setTimeout(function () {
                                    window.location.reload();
                                }, 500);
                            }else if(data.code == -7){
                                layer.msg(data.msg, {icon: 2});
                                window.setTimeout(function () {
                                    top.location = '{fun:ctr("../Login")}';
                                }, 1000);
                            }else{
                                layer.msg(data.msg, {icon: 2});
                            }
                        },
                        error: function(data){
                            layer.msg('数据异常，操作失败！');
                        },
                    });
                });
            }
        });

    });
</script>
</body>
</html>
